package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Setmeal;
import com.itheima.service.ISetmealService;
import com.itheima.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    @GetMapping("/{id}")
    public Result getSetmeal(@PathVariable Long id){
        return setmealService.getSetmeal(id);
    }

    @PostMapping
    public Result saveSetmeal(@RequestBody Setmeal setmeal){
        return Result.success(setmealService.save(setmeal));
    }

    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,@RequestParam Long ids){
        return setmealService.status(status,ids);
    }

    @GetMapping("/list")
    public Result list(@RequestParam Long categoryId,@RequestParam Integer status){
        return setmealService. setmealList(categoryId,status);
    }

    @GetMapping("/page")
    public Result getPage(@RequestParam(value = "page") int page, @RequestParam(value = "pageSize")  int pageSize, @RequestParam(value = "name" ,required = false) String name){
        IPage setmealServicePage = setmealService.getPage(page, pageSize ,name);
        if (page >setmealServicePage.getPages()){
            setmealServicePage = setmealService.getPage((int) setmealServicePage.getPages(), pageSize ,name);
        }
        return Result.success(setmealServicePage);
    }
}
